#include "DepartClinicianView.h"
#include "ui_DepartClinicianForm.h"
#include <QDebug>
#include <qsqlquery.h>
#include <QScrollBar>
#include "GlobalApi/SqliteDb.h"
#include "Auxiliary/View/MessageBoxDialog.h"

 enum WgtTab
 {
    DepartTab = 0,
    ClinicianTab,
 };

DepartClinicianView::DepartClinicianView(QWidget *parent) : QWidget(parent),
  ui(new Ui::DepartClinician)
{

   ui->setupUi(this);
   connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentChangeHandle(int))); // Tab页切换信号槽

   // 设置科室表
   departTbv = new TableWgt;
   ui->gLayout_Depart->addWidget(departTbv);
   departTbv->setFixedSize(500,300);;
   departTbv->sethHeaderHeight(30);
   departTbv->setvHeaderWidth(50);
   departTbv->showWithVerControlBtn(true);
   departTbv->showWithHorControlBtn(false);
   departTbv->setTableEditTri(true);

   model_Depart = new MySqlTableModel(this, SqliteDb::getInstance()->getDB());
   model_Depart->setTable("Department");
   model_Depart->select();
   model_Depart->setEditStrategy(QSqlTableModel::OnManualSubmit);
   departTbv->getTableView()->setModel(model_Depart);
   model_Depart->setHeaderData(0, Qt::Horizontal, tr("名称"));

   pDelegate = new LineEditDelegate(this);
   QString conditions = "^.{1,20}$";
   pDelegate->getlineeditconditions(conditions);
   departTbv->getTableView()->setItemDelegate(pDelegate);


   // 设置送检表
   clinicianTbv = new TableWgt;
   ui->gLayout_Clinician->addWidget(clinicianTbv);
   clinicianTbv->setFixedSize(500,300);;
   clinicianTbv->sethHeaderHeight(30);
   clinicianTbv->setvHeaderWidth(50);
   clinicianTbv->showWithVerControlBtn(true);
   clinicianTbv->showWithHorControlBtn(false);
   clinicianTbv->setTableEditTri(true);

   model_Clinician = new MySqlTableModel(this, SqliteDb::getInstance()->getDB());
   model_Clinician->setTable("Clinician");
   model_Clinician->select();
   model_Clinician->setEditStrategy(QSqlTableModel::OnManualSubmit);
   clinicianTbv->getTableView()->setModel(model_Clinician);
   model_Clinician->setHeaderData(0, Qt::Horizontal, tr("名称"));
   clinicianTbv->getTableView()->setItemDelegate(pDelegate);

}

DepartClinicianView::~DepartClinicianView()
{
    delete ui;
}

void DepartClinicianView::showEvent(QShowEvent *e)
{

}

void DepartClinicianView::hideEvent(QHideEvent *e)
{

}

void DepartClinicianView::mousePressEvent(QMouseEvent *e)
{

}

void DepartClinicianView::getgrade()
{
    grade_now = Grade_developer;

    if( grade_now == Grade_user  )
    {
        ui->Btn_add->hide();
        ui->Btn_del->hide();
        departTbv->getTableView()->setEditTriggers(QAbstractItemView::NoEditTriggers);
        clinicianTbv->getTableView()->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
    else
    {
        ui->Btn_add->show();
        ui->Btn_del->show();

        departTbv->getTableView()->setEditTriggers(QAbstractItemView::SelectedClicked);
        clinicianTbv->getTableView()->setEditTriggers(QAbstractItemView::SelectedClicked);
    }
}

void DepartClinicianView::saveSettings()
{

}

void DepartClinicianView::depart_add()
{
    int departMaxNum;
    departMaxNum = model_Depart->rowCount();

    if(departMaxNum == 0)
    {
        model_Depart->insertRow(departMaxNum);  // 插入新的一行
        model_Depart->setData(model_Depart->index(departMaxNum, 0), "");
        model_Depart->submitAll();
        departTbv->getTableView()->selectRow(departMaxNum);
        ui->Btn_del->setEnabled(true);
        return;
    }

    if( departMaxNum >= 100 )
    {
        MessageBoxDialog *messageBox = new MessageBoxDialog(this,\
                                 QString(tr("科室已满!")));
        messageBox->exec();
        return;
    }
    departMaxNum --;
    if(model_Depart->data(model_Depart->index(departMaxNum, 0)).toString().trimmed() == "")
    {
        return;
    }

     departMaxNum++;
     model_Depart->insertRow(departMaxNum);  // 插入新的一行
     model_Depart->setData(model_Depart->index(departMaxNum, 0), "");

     if(model_Depart->submitAll()){
         model_Depart->database().commit();
      } else {
         model_Depart->database().rollback();
      }

     departTbv->getTableView()->selectRow(departMaxNum);  // 选中改行

    if(departMaxNum > 10)
    {

        int maxvalue =  departTbv->getTableView()->verticalScrollBar()->maximum();
        departTbv->getTableView()->verticalScrollBar()->setValue(maxvalue);
    }
    else
    {
        ;
    }
}

void DepartClinicianView::depart_del()
{
    int rowNum = model_Depart->rowCount();

    if(rowNum <= 1)
    {
        ui->Btn_del->setEnabled(false);
    }

    int curRow = departTbv->getTableView()->currentIndex().row();
    model_Depart->removeRow(curRow);

    if(model_Depart->submitAll()){
        model_Depart->database().commit();
     } else {
        model_Depart->database().rollback();
     }
}

void DepartClinicianView::clinician_add()
{

    int clinicianMaxNum;
    clinicianMaxNum = model_Clinician->rowCount();

    if(clinicianMaxNum == 0)
    {
        model_Clinician->insertRow(clinicianMaxNum);  // 插入新的一行
        model_Clinician->setData(model_Clinician->index(clinicianMaxNum, 0), "");
        model_Clinician->submitAll();
        clinicianTbv->getTableView()->selectRow(clinicianMaxNum);
        ui->Btn_del->setEnabled(true);
        return;
    }

    if( clinicianMaxNum >= 100 )
    {
        MessageBoxDialog *messageBox = new MessageBoxDialog(this,\
                                 QString(tr("送检者已满!")));
        messageBox->exec();
        return;
    }
    clinicianMaxNum --;
    if(model_Clinician->data(model_Clinician->index(clinicianMaxNum, 0)).toString().trimmed() == "")
    {
        return;
    }

     clinicianMaxNum++;
     model_Clinician->insertRow(clinicianMaxNum);  // 插入新的一行
     model_Clinician->setData(model_Depart->index(clinicianMaxNum, 0), "");

     if(model_Clinician->submitAll()){
         model_Clinician->database().commit();
      } else {
         model_Clinician->database().rollback();
      }

     clinicianTbv->getTableView()->selectRow(clinicianMaxNum);  // 选中改行

    if(clinicianMaxNum > 10)
    {
        int maxvalue =  clinicianTbv->getTableView()->verticalScrollBar()->maximum();
        clinicianTbv->getTableView()->verticalScrollBar()->setValue(maxvalue);
    }
    else
    {
        ;
    }
}

void DepartClinicianView::clinician_del()
{
    int rowNum = model_Clinician->rowCount();

    if(rowNum <= 1)
    {
        ui->Btn_del->setEnabled(false);
    }

    int curRow = clinicianTbv->getTableView()->currentIndex().row();
    model_Clinician->removeRow(curRow);

    if(model_Clinician->submitAll()){
        model_Clinician->database().commit();
     } else {
        model_Clinician->database().rollback();
     }
}


void DepartClinicianView::on_Btn_del_clicked()
{
    int index = ui->tabWidget->currentIndex();

    MessageBoxDialog *messageBox = new MessageBoxDialog(this,\
                             QString(tr("是否删除当前记录？")));
    int result = messageBox->exec();
    if(result == QDialog::Accepted)
    {
           ;

    } else {
           return;
    }


    if( index == DepartTab)
    {
        depart_del();
    }
    else if(index == ClinicianTab)
    {
       clinician_del();
    }

}

void DepartClinicianView::on_Btn_add_clicked()
{
    int index = 0;

    index = ui->tabWidget->currentIndex();

    if(index == DepartTab)
    {
        depart_add();
    }
    else if(index == ClinicianTab)
    {
        clinician_add();
    }
}

void DepartClinicianView::currentChangeHandle(int)
{

}

void DepartClinicianView::myClearFocus()
{
}

void DepartClinicianView::dept_ScrollClicked(int value)
{
}

void DepartClinicianView::clinician_ScrollClicked(int value)
{
}

void DepartClinicianView::dept_Rangechange(int min_, int max_)
{
}

void DepartClinicianView::clinician_Rangechange(int min_, int max_)
{
}

bool DepartClinicianView::modules_change_save_depart()
{
}

bool DepartClinicianView::modules_change_save_clinician()
{
}

void DepartClinicianView::close_softKeyboard_handle()
{
}

void DepartClinicianView::delbuttonisEnabled(int index)
{
}

int DepartClinicianView::departments_compare_param_to_bak()
{
}

int DepartClinicianView::depart_judge_name_if_repeat()
{
}

int DepartClinicianView::clinician_compare_param_to_bak()
{
}

int DepartClinicianView::clinician_judge_name_if_repeat()
{
}
